@page "/admin/logout"
@using MyBlogServer.Services
@using Microsoft.AspNetCore.Authentication
@inject NavigationManager Navigation
@inject CustomAuthenticationStateProvider AuthStateProvider
@inject IJwtAuthService JwtAuthService

@rendermode InteractiveServer

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await ProcessLogout();
        }
    }

    private async Task ProcessLogout()
    {
        try
        {
            Console.WriteLine("用户登出");
            
            // 清除存储的JWT Token
            await JwtAuthService.ClearTokenAsync();
            
            // 通知认证状态提供者用户已登出
            AuthStateProvider.NotifyUserLoggedOut();
            
            // 延迟一段时间确保状态更新完成
            await Task.Delay(100);
            
            // 跳转到登录页面
            Navigation.NavigateTo("/admin/login", forceLoad: true);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"登出时发生错误: {ex.Message}");
            // 即使出错也尝试跳转到登录页面
            Navigation.NavigateTo("/admin/login", forceLoad: true);
        }
    }
}